package main

import (
	"fmt"
	"github.com/sciter-sdk/go-sciter"
	"github.com/sciter-sdk/go-sciter/window"
	"log"
	"strconv"
	"time"
	"os/exec"
	"os"
	"strings"
	"io"
)

func main() {
	//先判断当前系统是否是64位系统
	bit := 32 << (^uint(0) >> 63)
	flag,err := PathExists("C:\\Windows\\System32\\sciter.dll")
	if err!= nil {
		fmt.Println(err)
	}
	//如果不存在
	if !flag {
		copyFlag , err2 := CopyFile("./statics/"+strconv.Itoa(bit)+"/sciter.dll","C:\\Windows\\System32\\sciter.dll")

		if err2 != nil {
			fmt.Println(err2)
		}

		fmt.Println(copyFlag)
	}
	//创建window窗口
	//参数一表示创建窗口的样式
	//SW_TITLEBAR 顶层窗口，有标题栏
	//SW_RESIZEABLE 可调整大小
	//SW_CONTROLS 有最小/最大按钮
	//SW_MAIN 应用程序主窗口，关闭后其他所有窗口也会关闭
	//SW_ENABLE_DEBUG 可以调试
	//参数二表示创建窗口的矩形
	w, err := window.New(sciter.SW_TITLEBAR|
		sciter.SW_RESIZEABLE|
		sciter.SW_CONTROLS|
		sciter.SW_MAIN|
		sciter.SW_ENABLE_DEBUG,
		//给窗口设置个大小
		&sciter.Rect{Left: 200, Top: 100, Right: 1000, Bottom: 800})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(getCurrentPath())
	//加载文件
	w.LoadFile("./statics/index.html")
	w.SetTitle("小堤工具")
	root,_ := w.GetRootElement()
	//设置标题
	setElementHandlers(root)

	//显示窗口
	w.Show()
	//运行窗口，进入消息循环
	w.Run()
}


//设置元素的处理程序
func setElementHandlers(root *sciter.Element) {
	nowTime ,_ := root.SelectById("now_time")
	ticker := time.NewTicker(time.Second)
	go setTime(nowTime,ticker)
	nowTimeStr := sciter.NewValue(strconv.FormatInt(time.Now().Unix(),10))
	nowTime.SetValue(nowTimeStr)
	stop, _ := root.SelectById("stop")
	start , _ := root.SelectById("start")
	//设置背景图片
	body , _ := root.SelectById("body")
	imgStr := getCurrentPath()+"/statics/img/background.png"

	body.SetStyle("background-image","url("+imgStr+")")
	//处理元素简单点击事件
	stop.OnClick(func() {
		ticker.Stop()
		stop.SetStyle("display","none")
		start.SetStyle("display","block")
	})

	start.OnClick(func() {
		ticker = time.NewTicker(time.Second)
		go setTime(nowTime,ticker)
		stop.SetStyle("display","block")
		start.SetStyle("display","none")
	})


}

/**
设置时间
 */
func setTime(nowTime *sciter.Element,ticker *time.Ticker){
		for{
			select{
			case <-ticker.C:
				val := sciter.NewValue(strconv.FormatInt(time.Now().Unix(),10))
				nowTime.SetValue(val)
				//nowTime.SetText(strconv.FormatInt(time.Now().Unix(),10))
				//nowTime.SetAttr("value",strconv.FormatInt(time.Now().Unix(),10))
			}
		}
}



//获取当前路径
func getCurrentPath() string {
	s, err := exec.LookPath(os.Args[0])
	checkErr(err)
	i := strings.LastIndex(s, "\\")
	path := string(s[0 : i+1])
	return path
}

//检查是否存在错误
func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}

//复制文件  需要考虑权限问题
func CopyFile(src, des string) (w int64, err error) {
	srcFile, err := os.Open(src)
	if err != nil {
		fmt.Println(err)
	}
	defer srcFile.Close()

	desFile, err := os.Create(des)
	if err != nil {
		fmt.Println(err)
	}
	defer desFile.Close()

	return io.Copy(desFile, srcFile)
}
//文件是否存在
func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}